package study.kit.eggs.result;

import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Map;

@Slf4j
public final class Results {

    private static final Map<String, Object> EMPTY_DATA = Collections.emptyMap();

    private Results() {
    }

    /**
     * ==============================
     * 成功
     * ==============================
     */
    public static <T> Result<T> ok() {
        return ok(null);
    }

    public static <T> Result<T> ok(T data) {
        return build(100, "成功", data);
    }

    /**
     * ==============================
     * 失败
     * ==============================
     */
    public static <T> Result<T> error() {
        return error(999, "系统异常");
    }

    public static <T> Result<T> error(int code, String desc) {
        return error(code, desc, null);
    }

    public static <T> Result<T> error(int code, String desc, T data) {
        return build(code, desc, data);
    }

    /**
     * 构建
     */
    private static <T> Result build(int code, String desc, T data) {
        Result result = new Result();
        result.setCode(code);
        result.setDesc(desc);
        if (data == null) {
            data = (T) EMPTY_DATA;
        }
        result.setData(data);
        return result;
    }
}
